/*
 *  Copyright 2004 Blandware (http://www.blandware.com)
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.blandware.atleap.webapp.taglib.core.grid;

import com.blandware.atleap.webapp.util.core.RequestUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.taglib.TagUtils;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
import java.io.StringWriter;


/**
 * <p>Tag for marking column as filterable (Type: number). This will generate a
 * link for displaying number filter window.
 * <br />
 * This tag is only valid when nested within <em>grid</em> tag
 * </p>
 * <p>
 * Some attributes are inherited from {@link BaseFilterTag}.
 * </p>
 * <p>
 * This tag must either be contained in <em>column</em> tag or have
 * <b>fieldName</b> specified.
 * </p>
 * <p><a href="NumberFilterTag.java.html"><i>View Source</i></a></p>
 *
 * @author Sergey Zubtsovskiy <a href="mailto:sergey.zubtsovskiy@blandware.com">&lt;sergey.zubtsovskiy@blandware.com&gt;</a>
 * @version $Revision: 1.12 $ $Date: 2006/08/03 10:08:20 $
 * @jsp.tag name="numberFilter"
 * body-content="scriptless"
 * @see com.blandware.atleap.webapp.taglib.core.grid.GridTag
 */
public class NumberFilterTag extends BaseFilterTag {
	protected transient final Log log = LogFactory.getLog(NumberFilterTag.class);

    /**
     * Processes the tag
     *
     * @throws JspException
     * @throws IOException
     */
	public void doTag() throws JspException, IOException {

		PageContext pageContext = (PageContext) getJspContext();

		// This tag is only valid when nested within 'grid' tag, so check this
		GridTag parentGridTag = (GridTag) findAncestorWithClass(this, GridTag.class);

		if ( parentGridTag == null ) {
			JspTagException e = new JspTagException("Parent tag is invalid! This tag is only valid when nested within 'grid' tag");
			throw e;
		}

		// Get field name and field key from parent 'column' tag
		ColumnTag parentColumnTag = (ColumnTag) findAncestorWithClass(this, ColumnTag.class);
		if ( parentColumnTag != null ) {
			if ( fieldName == null || fieldName.length() == 0 ) {
				fieldName = parentColumnTag.getFieldName();
			}

			if ( fieldKey == null || fieldKey.length() == 0 ) {
				fieldKey = parentColumnTag.getFieldKey();
			}
		}

		if ( fieldName == null || "".equals(fieldName) ) {
			// Error: there is no ancestor 'column' tag and no fieldName attribute is specified
			String errorMessage = "There is no ancestor 'column' tag and no fieldName attribute is specified!";
			if ( log.isErrorEnabled() ) {
				log.error(errorMessage);
			}
			JspTagException e = new JspTagException(errorMessage);
			throw e;
		}


		// Get page URL and create hyperlink to show filter window

		String pageUrl = RequestUtil.getPageUrl(parentGridTag);

		String scriptCall = "showNumberFilterWindow(\'" + fieldName + "\', \'" + parentGridTag.getName() +
		        "\', \'" + pageUrl + "\'";

		if ( fieldKey == null ) {
			fieldKey = "";
		}
		scriptCall += ", \'" + fieldKey + "\'";

		if ( rowIterators == null ) {
			rowIterators = "";
		}
		scriptCall += ", \'" + rowIterators + "\'";


		scriptCall += ");";

		if ( onclick != null && onclick.length() != 0 ) {
			int k = onclick.indexOf("return false");
			if ( k != -1 ) {
				onclick = onclick.substring(0, k);
			}
		} else {
			onclick = new String();
		}
		onclick += scriptCall + " return false;";
		StringWriter sw = new StringWriter();
		StringBuffer sb = sw.getBuffer();
		sb.append("<a href=\"javascript:void(0)\"").append(prepareAttributes()).append(">");
		getJspBody().invoke(sw);
		sb.append("</a>");
		TagUtils.getInstance().write(pageContext, sw.toString());
	}

}
